package com.yunhai.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * 网关统一处理跨域问题
 * **/
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){

        //配置跨域
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许任意请求头
        corsConfiguration.addAllowedHeader("*");
        //允许任意的请求方法
        corsConfiguration.addAllowedMethod("*");
        //允许哪些请求来源
//        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOriginPattern("*");
        //是否允许携带 cookie
        corsConfiguration.setAllowCredentials(true);

        //使用响应式包
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }

}
